import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import stores from "@/stores";
import Manage from '../views/Manage.vue'
import User from '../views/User.vue'
import Merchant from '../views/Merchant.vue'
import Home from '../views/Home.vue'
import Notice from '../views/Notice.vue'
import Order from '../views/Order.vue'
import Room from '../views/Room.vue'
import Condition from '../views/Condition.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Person from '../views/Person.vue'
import File from '../views/File.vue'
import Remark from '../views/Remark.vue'
import Menu from '../views/Menu.vue'
import ChessFriends from '../views/ChessFriends.vue'
import P404 from '../views/404.vue'
const modulesR = import.meta.glob(`../views/**/*.vue`)

const routes = [
    {
      path: '/',
      name: 'manage',
      component: Manage,
      redirect:'/home',
      children:[
          {
            path: 'user',
            name: '用户管理',
            component: User,
          },
          {
            path: 'merchant',
            name: '商家管理',
            component: Merchant,
          },
          {
            path: 'room',
            name: '棋牌室管理',
            component: Room,
          },
          {
            path: 'condition',
            name: '空调管理',
            component: Condition,
          },
          {
            path: 'notice',
            name: '公告栏管理',
            component: Notice,
          },
          {
            path: 'chessFriends',
            name: '牌友圈模块',
            component: ChessFriends,
          },
          {
            path: 'order',
            name: '订单模块',
            component: Order,
          },
          // {
          //   path: 'merchant',
          //   name: '商家管理',
          //   component: Merchant,
          // },
          {
            path: 'Remark',
            name: '评论模块',
            component: Remark,
          },
          {
            path: 'menu',
            name: '菜单管理',
            component: Menu,
          },
          {
            path: 'home',
            name: '首页',
            component: Home,
          },
          {
            path: 'person',
            name: '个人信息',
            component: Person,
          },
          {
            path: 'file',
            name: '文件',
            component: File,
          },
      ]},
      {
        path: '/login',
        name: 'Login',
        component: Login,
      },
      {
        path: '/register',
        name: 'Register',
        component: Register,
      },

    ]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})





//   // 提供一个重置路由的方法
// export const resetRouter = () => {
//   router.matcher = createRouter({
//     history: createWebHistory(import.meta.env.BASE_URL),
//     routes
//   })
// }

// //刷新会重置路由
// //console.log("重置路由");
//   router.addRoute(routes)



    // 全局守卫，前置守卫（在路由跳转之间进行判断）
    router.beforeEach((to,from,next)=>{
        // let token = store.state.user.token;
        // let name = store.state.user.userInfo.name;

      //console.log('to',to);

        localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称，为了在Header组件中去使用
        stores.commit("headerAbout/setPath")  // 触发store的数据更新

        //路由验证
        let mUser = localStorage.getItem("mUser")

        // if(to.path === "/register" && mUser === null){  //未登录，但是要去登录页
        //   next()
        //   return
        // }

        if(to.name === "Register" && mUser === null){  
         
          next()
          return
        }
          if(to.path !== "/login" && mUser === null  ){
            //console.log(mUser);


            next({name:'Login'})
          }else{
            if(to.name === "Register"){  
              next({name:"manage"})
              return
            }else{
              next()
            }
            next()
          }
       
    })



export default router


